package com.sleepcamel.resteasyConfigurator.view.detail;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;

import com.sleepcamel.rest.domain.BaseMethod;
import com.sleepcamel.rest.domain.method.RESTMethod;
import com.sleepcamel.rest.domain.method.parameter.MethodParameter;
import com.sleepcamel.rest.domain.method.parameter.RESTMethodParameter;

public class MethodParameterViewList extends Composite {

	private ListViewer parameterListViewer;
	private java.util.List<MethodParameter> list;
	
	private NonRESTMethodParameterView nonRESTMethodParameterView;
	private RESTMethodParameterView methodParameterView;

	private Class<? extends BaseMethod> method;

	public MethodParameterViewList(Composite parent, int style) {
		super(parent, style);
	}
	
	private void setBackground(Control control) {
		control.setBackground(Display.getDefault().getSystemColor(
				SWT.COLOR_WHITE));
	}

	private void createControl() {
		setBackground(this);
		GridLayout gridLayout = new GridLayout(3, false);
		setLayout(gridLayout);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.heightHint = 78;
		
		parameterListViewer = new ListViewer(this, SWT.BORDER | SWT.V_SCROLL);
		parameterListViewer.setContentProvider(ArrayContentProvider.getInstance());
		parameterListViewer.setLabelProvider(new MethodParameterLabelProvider());
		parameterListViewer.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent paramDoubleClickEvent) {
				IStructuredSelection selection = (IStructuredSelection) paramDoubleClickEvent.getSelection();
				if ( !selection.isEmpty() ){
					Object firstElement = selection.getFirstElement();
					if ( firstElement instanceof RESTMethodParameter ){
						methodParameterView.setModelData((RESTMethodParameter) selection.getFirstElement());
					}else if ( firstElement instanceof MethodParameter ){
						nonRESTMethodParameterView.setModelData((MethodParameter) selection.getFirstElement());
					}
					list.remove(firstElement);
					parameterListViewer.refresh();
				}
			}
		});

		List parameterListView = parameterListViewer.getList();
		data = new GridData(GridData.FILL_BOTH);
		data.heightHint = 78;
		data.widthHint = 180;
		parameterListView.setLayoutData(data);
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);

		nonRESTMethodParameterView = new NonRESTMethodParameterView(this, SWT.NONE);
		data = new GridData(GridData.FILL_BOTH);
		data.heightHint = 25;
		data.widthHint = 180;
		nonRESTMethodParameterView.setLayoutData(data);
		setBackground(nonRESTMethodParameterView);
		nonRESTMethodParameterView.setAddCallback(new Action(){
			@Override
			public void run() {
				list.add(nonRESTMethodParameterView.getParameter());
				parameterListViewer.refresh();
			}
		});
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);

		if ( method.equals(RESTMethod.class) ){
			methodParameterView = new RESTMethodParameterView(this, SWT.NONE);
			data = new GridData(GridData.FILL_BOTH);
			data.heightHint = 25;
			data.widthHint = 180;
			methodParameterView.setLayoutData(data);
			setBackground(methodParameterView);
			methodParameterView.setAddCallback(new Action(){
				@Override
				public void run() {
					list.add(methodParameterView.getParameter());
					parameterListViewer.refresh();
				}
			});
			new Label(this, SWT.NONE);
			new Label(this, SWT.NONE);
		}
	}

	@SuppressWarnings("rawtypes")
	public void setMethodClass(Class<? extends BaseMethod> class1) {
		this.method = class1;
		createControl();		
	}

	public void setMethodParameters(java.util.List<MethodParameter> list) {
		this.list = list;
		parameterListViewer.setInput(list);
	}
}

